<!--
 * @Date: 2024-07-01 09:53:59
 * @Author: LiSong
 * @LastEditors: LiSong
 * @LastEditTime: 2024-08-12 18:16:53
 * @Description: 授权跳转中转页
-->
<template>
  <div class="oauth-wrapper">
    <div class="oauth-info-wrapper" v-if="!oauthStore.valid">
      <div class="oauth-info-card">
        <div class="oauth-info-title">
          <span>系统提示</span>
        </div>
        <div class="oauth-info-body">
          <span>{{ oauthStore.errorMessage }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRoute, useRouter } from 'vue-router';

import { Session, NextLoading } from '@/utils'

import { useSystemStore } from '@/stores/modules/store-system';
import { initBackEndControlRoutes } from '@/router/backEnd';


const router = useRouter();
const { query: routerQuery } = useRoute();

const oauthStore = reactive({
  valid: false,
  loading: false,
  errorMessage: '',
})

const loadOauth = async () => {
  NextLoading.done();
  oauthStore.loading = true
  // 验证是否存在token参数
  if (!validateToken()) return
  // 验证是否存在跳转路径
  if (!validateJumpPath()) return
  // 请求参数
  const reqParams = { ...routerQuery, isMenu: false }
  const signInfo = await useSystemStore().signInExtCall(reqParams);
  if (signInfo.flag) {
    let isNoPower = await initBackEndControlRoutes(false);
    signInSuccess(isNoPower)
  } else {
    setValidInfo(false, signInfo.message)
  }
  oauthStore.loading = false
}

/**
 * 登录成功以后执行
 * 
 * @param isNoPower 是否有权限
 */
const signInSuccess = (isNoPower) => {
  const { token, itoken, path } = routerQuery
  const { extCallConfig } = useSystemStore()
  if (isNoPower) {
    useSystemStore().clearAllCache(false)
    Session.clear();
  } else {
    // 处理地址上原有参数
    let prveQuery = getPathParam(path)
    let nextQuery = JSON.parse(JSON.stringify(routerQuery))
    delete nextQuery.path
    let finalQuery = {
      token: token || itoken,
      ...prveQuery,
      ...nextQuery,
      isCall: extCallConfig.isCall,
      isMenu: extCallConfig.isMenu,
    }
    let finalPath = path
    if (finalPath && finalPath.includes('?')) {
      finalPath = finalPath.split('?')[0]
    }
    router.replace({
      path: finalPath || '/404',
      query: finalQuery
    });
  }
}

/**
 * 获取跳转地址传递参数
 */
const getPathParam = (path) => {
  let result = {}
  if (path.includes('?')) {
    let paramStr = path.split('?')[1]
    let paramItems = paramStr.split('&')
    paramItems.forEach(itemStr => {
      let item = itemStr.split('=')
      result[item[0]] = item[1]
    });
  }
  return result
}

/**
 * 设置校验状态以及信息
 *
 * @param valid 是否校验通过
 * @param message 校验提示信息
 */
const setValidInfo = (valid=false, message='') => {
  oauthStore.valid = valid
  oauthStore.errorMessage = message
}

/** 校验token信息 */
const validateToken = () => {
  // 校验
  if (routerQuery.hasOwnProperty('token')) {
    if (routerQuery.token) {
      setValidInfo(true, '【token】存在且有值！')
    } else {
      setValidInfo(false, '【token】内容不能为空, 请确认！')
    }
  } else {
    if (routerQuery.hasOwnProperty('itoken')) {
      if (routerQuery.itoken) {
        setValidInfo(true, '【itoken】存在且有值！')
      } else {
        setValidInfo(false, '【itoken】内容不能为空, 请确认！')
      }
    } else {
      setValidInfo(false, '【token|itoken】参数不存在, 请确认！')
    }
  }
  return oauthStore.valid
}
/** 校验跳转路径信息 */
const validateJumpPath = () => {
  if(routerQuery.hasOwnProperty('path')) {
    if (routerQuery.path) {
      setValidInfo(true, '【path】存在且有值！')
    } else {
      setValidInfo(false, '【path】内容不能为空, 请确认！')
    }
  } else {
    setValidInfo(false, '【path】参数不存在, 请确认！')
  }
  return oauthStore.valid
}


loadOauth()
</script>

<style lang="scss" scoped>
.oauth-info {
  &-wrapper {
    width: 400px;
    margin: 0 auto;
    padding-top: 100px;
  }
  &-card {
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(12,12,12,0.09);
  }
  &-title {
    padding: 0 10px;
    color: #fff;
    border-radius: 4px 4px 0 0;
    background-color: #9d9790;
    height: 40px;
    line-height: 40px;
  }
  &-body {
    padding: 10px;
    border: 4px solid #9d9790;
    border-top: none;
    height: 150px;
    border-radius: 0 0 4px 4px;
  }
}
</style>
